/***************************************************************************
 *            binnote.h
 *
 *  Sat Nov 19 21:13:30 2005
 *  Copyright  2005  Robin Shao
 *  swjbook@yahoo.com
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#ifndef _BINNOTE_H
#define _BINNOTE_H
#define INT_SIZE (sizeof(int))
#include <stdio.h>
#include <sys/stat.h>
#include "Unit.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int FILEVERSION=2;//used to be 1;
const string FILEMARK="QQDATA";//this will be fixed throug

const int OFF_FILEMARK=0;//0 means at the begin of the file,it's for verify.
const int OFF_FILEVERSION=6+INT_SIZE;//sizeof(int)(=4)+ sizeof("QQDATA")(=6)
const int OFF_QQNUM=OFF_FILEVERSION+INT_SIZE;//qqnum use int,max limited at 0xFFFFFFFF on 32bit machine
const int OFF_LASTPOS=OFF_QQNUM+INT_SIZE;
const int OFF_LASTDATE=OFF_LASTPOS+INT_SIZE;
const int OFF_COUNTDATE=OFF_LASTDATE+INT_SIZE;

const int INDEXSTART=OFF_COUNTDATE+INT_SIZE;//const pinters
const int INDEXEND=INDEXSTART+2000*INT_SIZE;//const pointers
//1000 date+ 1000 offset =2000 int
const int BODYSTART=INDEXEND+100;//100 byte reserved for future use.
const int FIRSTPOS=BODYSTART;

class binnote
{
	public:
		binnote();
		~binnote();
		binnote(const int user_id,const int friend_id);
	//only user qq num is needed.
		int file_exists (const char *fileName);
		bool write_msg(const int qqnum,
					 const string nickname,
					 const int time_msg,
					 const string msg);
		int read_msg(int &qqnum,
					 string &nickname,
					 int &time_msg,
					 string &msg);
		bool write_head();
		bool read_head();
		int write_it(int i,int offset=-1);
		int read_it(int &i,int offset=-1);
		int write_it(const string &i,int offset=-1);
		int read_it(string & i,int offset=-1);
		bool export_txt(const string & txtname);
		void convert_time(int,string &);
		int date2int(int time_msg);
		int set_start_date(int);
		int set_end_date(int);
	//constructor with parameter 
	protected:
		char filename[255];
		fstream datafile;
		struct headinfo
		{
			int qqnum;
			int file_version;
			//int first_position;
			int last_position;
			int last_date;
			int date_count;
			struct dateindex
			{
				int date;
				int offset;
			}index[1000];
		}head;
		int read_offset;
};
		
#endif /* _BINNOTE_H */
